#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Biblioteca Table2CSV: conversor de tabela HTML para CSV
# Versão 0.2

import tempfile
import csv
from HTMLParser import HTMLParser

class Table2CSV(HTMLParser):

    get_data_flag = False
    data_collected = ""
    data_row = []

    def handle_starttag(self, tag, attrs):
        if tag == "tr":
            self.csv_line = []
        if tag in ["td", "th"]:
            self.get_data_flag = True
            self.data_collected = ""
        else:
            self.get_data_flag = False

    def handle_data(self, data):
        if self.get_data_flag:
            self.data_collected = self.data_collected + data

    def handle_endtag(self, tag):
        if tag == "tr":
            self.csv_writer.writerow(self.data_row)
            self.data_row = []
        if tag in ["td", "th"]:
            self.data_row.append(self.data_collected)
            self.data_collected = ""

    def read(self, html_table):
        self.html_table = html_table
        return len(self.html_table)

    def convert(self):
        self.file_descriptor = tempfile.TemporaryFile()
        self.csv_writer = csv.writer(self.file_descriptor, quoting=csv.QUOTE_ALL)
        self.feed(self.html_table)
        self.file_descriptor.seek(0)
        return self.file_descriptor
